package tw.cuttle.bean.holder;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import tw.cuttle.bean.Action;
import tw.cuttle.bean.ActionException;
import tw.cuttle.bean.Actions;
import tw.cuttle.bean.Form;
import tw.cuttle.bean.Process;
import tw.cuttle.bean.ProcessException;
import tw.cuttle.bean.Setting;
import tw.cuttle.bean.Template;

public class BeanHolder {
    private Setting setting;
    private Actions actions;
    private Map<String, Action> actionMap;
    private Map<String, Process> processMap;
    private Map<String, List<Process>> processListMap;
    private Map<String, Form> formMap;
    private Map<String, ActionException> actionExceptionMap;
    private Map<String, ProcessException> processExceptionMap;
    private Log LOGGER=LogFactory.getLog(BeanHolder.class);
    /**
     * @return the formMap
     */
    public Map<String, Form> getFormMap() {
        return formMap;
    }
    /**
     * @param formMap the formMap to set
     */
    public void setFormMap(Map<String, Form> formMap) {
        this.formMap = formMap;
    }
    /**
     * @return the processListMap
     */
    public Map<String, List<Process>> getProcessListMap() {
        return processListMap;
    }
    /**
     * @param processListMap the processListMap to set
     */
    public void setProcessListMap(Map<String, List<Process>> processListMap) {
        this.processListMap = processListMap;
    }
    /**
     * @param actionMap the actionMap to set
     */
    public void setActionMap(Map<String, Action> actionMap) {
        this.actionMap = actionMap;
    }
    /**
     * @param processMap the processMap to set
     */
    public void setProcessMap(Map<String, Process> processMap) {
        this.processMap = processMap;
    }
    /**
     * @return the setting
     */
    public Setting getSetting() {
        return setting;
    }
    /**
     * @param setting the setting to set
     */
    public void setSetting(Setting setting) {
        this.setting = setting;
    }
    /**
     * @return the actions
     */
    public Actions getActions() {
        return actions;
    }
    /**
     * @param actions the actions to set
     */
    public void setActions(Actions actions) {
        this.actions = actions;
    }
    public Action getAction(String id){
	return actionMap.get(id);
    }
    public Process getProcess(String id){
    	Process process=processMap.get(id);
    	if(process==null){
    		LOGGER.error("processId:"+id+" doesn't exist!");
    	}
    	return process;
    }
    public List<Process> getProcesses(String actionId){
	return processListMap.get(actionId);
    }
    public List<Process> getProcessesForAjax(String actionId){
	return processListMap.get(actionId+"_ajax");
    }
    public List<ActionException> getGlobalActionExceptions(){
	return getSetting().getGlobal().getActionExceptions();
    }
    public List<ProcessException> getGlobalProcessExceptions(){
	return getSetting().getGlobal().getProcessExceptions();
    }
    public List<ActionException> getActionExceptions(String actionId){
	//TODO
	return null;
    }
    public List<ProcessException> getProcessExceptions(Process process){
	
	//TODO
	return null;
    }
    public Template getTemplate(String id){
	
	for(Template template:getSetting().getLayout().getTemplates()){
		
	    if(template.getId().equals(id)){
		return template;
	    }
	}
	return null;
    }
    public Form getForm(String name){
	return this.getFormMap().get(name);
    }
}
